﻿Option Explicit On

'*******************************************************************
' Program: xmlVisio
' Author: Albert E Edlund
' Date:
'
' Purpose:
' Working with visio drawing in an xml format
'
'
'
'*******************************************************************

Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Text
Imports System.Text.StringBuilder

Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging

Imports System.IO
Imports System.Windows.Forms
Imports System.Xml
Imports System.Xml.Serialization
Imports System.Xml.Linq
Imports System.Xml.XPath

Imports <xmlns="http://schemas.microsoft.com/visio/2003/core">
Imports <xmlns:vx="http://schemas.microsoft.com/visio/2006/extension">
Imports <xmlns:v14="http://schemas.microsoft.com/office/visio/2010/extension">





Public Class frmSectionsConnections


#Region " Properties and Declarations "


    '
    ' where are we keeping the drawings
    '
    Private _dataPath As String
    Public Property dataPath As String
        Get
            dataPath = _dataPath
        End Get
        Set(value As String)
            _dataPath = value
        End Set
    End Property

    '
    ' what is the latest document
    '
    Public _xDoc As XDocument
    Public Property xDoc As XDocument
        Get
            Return Me._xDoc
        End Get
        Set(value As XDocument)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("Value", _
                    "xDoc is Null.")
            End If
            Me._xDoc = value
        End Set
    End Property


    Private _PgSource As XElement
    Public Property PgSource As XElement
        Get
            PgSource = _PgSource
        End Get
        Set(value As XElement)
            _PgSource = value
        End Set
    End Property


    Private _ShpSource As XElement
    Public Property ShpSource As XElement
        Get
            ShpSource = _ShpSource
        End Get
        Set(value As XElement)
            _ShpSource = value
        End Set
    End Property

    Private _SectionConnPtEnabled As Boolean
    Public Property sectionConnPtEnabled As Boolean
        Get
            sectionConnPtEnabled = _SectionConnPtEnabled
        End Get
        Set(value As Boolean)
            _SectionConnPtEnabled = value
        End Set
    End Property

    Private _sectionCtrlPtEnabled As Boolean
    Public Property sectionCtrlPtEnabled As Boolean
        Get
            sectionCtrlPtEnabled = _sectionCtrlPtEnabled
        End Get
        Set(value As Boolean)
            _sectionCtrlPtEnabled = value
        End Set
    End Property

    Private _SectionConnABCDEnabled As Boolean
    Public Property sectionConnABCDEnabled As Boolean
        Get
            sectionConnABCDEnabled = _SectionConnABCDEnabled
        End Get
        Set(value As Boolean)
            _SectionConnABCDEnabled = value
        End Set
    End Property

#End Region


    '
    '
    '
    Public Sub getConnections()

        getConnPtData(ShpSource, Me.lvConnPts)
        getCtrlPtData(ShpSource, Me.lvControlPoints)

    End Sub


    '
    '
    '
    Private Sub getConnPtData _
                (ByVal shapeSource As XElement, _
                 ByRef lvControl As ListView)

        If lvControl Is Nothing Then lvControl = Me.lvConnPts

        ' clear the control and setup the columns
        lvControl.Items.Clear()
        lvControl.Columns.Clear()
        lvControl.View = View.Details
        lvControl.AllowColumnReorder = True
        lvControl.FullRowSelect = True
        ' named indexed row
        lvControl.Columns.Add("Id", 50, HorizontalAlignment.Right)
        lvControl.Columns.Add("Ix", 50, HorizontalAlignment.Right)
        lvControl.Columns.Add("NameU", 100, HorizontalAlignment.Left)
        ' elements
        lvControl.Columns.Add("X", 50, HorizontalAlignment.Right)
        lvControl.Columns.Add("Y", 50, HorizontalAlignment.Right)
        lvControl.Columns.Add("DirX", 50, HorizontalAlignment.Right)
        lvControl.Columns.Add("DirY", 50, HorizontalAlignment.Right)
        lvControl.Columns.Add("Type", 50, HorizontalAlignment.Right)
        lvControl.Columns.Add("AutoGen", 50, HorizontalAlignment.Right)
        lvControl.Columns.Add("Prompt", 50, HorizontalAlignment.Left)

        Dim lvItem As ListViewItem

        If sectionConnPtEnabled = False Then Exit Sub

        ' no drill down
        Dim qryConnection = shapeSource.<Connection>
        Dim intConnections As Integer = qryConnection.Count

        For Each conn In qryConnection

            Dim intID As Integer = -1
            Dim intIX As Integer = -1
            Dim strNameU As String = "NoNameU"
            Dim strX As String = "NoX"
            Dim strY As String = "NoY"
            Dim strDirX As String = "NoDirX"
            Dim strDirY As String = "NoDirY"
            Dim strType As String = "NoType"
            Dim strAutoGen As String = "NoAutoGen"
            Dim strPrompt As String = "NoPropmpt"

            ' first the attributes 
            Dim listOfAttributes As IEnumerable(Of XAttribute) = _
                    From att In conn.Attributes() _
                    Select att
            For Each att As XAttribute In listOfAttributes
                ' Console.WriteLine("{0}={1}", att.Name.LocalName, att.Value)
                Select Case LCase(att.Name.LocalName)
                    Case "id"
                        intID = CInt(att.Value)
                    Case "ix"
                        intIX = CInt(att.Value)
                    Case "nameu"
                        strNameU = CStr(att.Value)
                End Select
            Next att

            ' now the element data
            Dim listOfElements As IEnumerable(Of XElement) = _
                From elem In conn.Elements() _
                Select elem

            If listOfElements.Count > 0 Then
                For Each elem As XElement In listOfElements
                    ' Console.WriteLine("Elem {0}={1}", elem.Name.LocalName, elem.Value)
                    Select Case LCase(elem.Name.LocalName)
                        Case "x"
                            strX = CStr(elem.Value)
                            Exit Select
                        Case "y"
                            strY = CStr(elem.Value)
                            Exit Select
                        Case "dirx"
                            strDirX = CStr(elem.Value)
                            Exit Select
                        Case "diry"
                            strDirY = CStr(elem.Value)
                            Exit Select
                        Case "type"
                            strType = CStr(elem.Value)
                            Exit Select
                        Case "autogen"
                            strAutoGen = CStr(elem.Value)
                            Exit Select
                        Case "prompt"
                            strPrompt = CStr(elem.Value)
                    End Select
                Next elem
            End If ' test to make sure we got at least one 

            lvItem = New ListViewItem(intID)
            lvItem.SubItems.Add(CStr(intIX))
            lvItem.SubItems.Add(strNameU)
            lvItem.SubItems.Add(strX)
            lvItem.SubItems.Add(strY)
            lvItem.SubItems.Add(strDirX)
            lvItem.SubItems.Add(strDirY)
            lvItem.SubItems.Add(strType)
            lvItem.SubItems.Add(strAutoGen)
            lvItem.SubItems.Add(strPrompt)

            ' add it to the listview on the data page
            lvControl.Items.Add(lvItem)
            System.Windows.Forms.Application.DoEvents()

        Next conn

        lvControl.Refresh()

    End Sub

    '
    '
    '
    Private Sub getCtrlPtData _
                (ByVal shapeSource As XElement, _
                ByRef lvControl As ListView)

        If lvControl Is Nothing Then lvControl = Me.lvControlPoints
        Dim lvItem As ListViewItem


        ' clear the control and setup the columns
        lvControl.Items.Clear()
        lvControl.Columns.Clear()
        lvControl.View = View.Details
        lvControl.AllowColumnReorder = True
        lvControl.FullRowSelect = True
        ' named indexed row
        lvControl.Columns.Add("Id", 50, HorizontalAlignment.Right)
        lvControl.Columns.Add("Ix", 50, HorizontalAlignment.Right)
        lvControl.Columns.Add("NameU", 100, HorizontalAlignment.Left)
        ' elements
        lvControl.Columns.Add("X", 50, HorizontalAlignment.Right)
        lvControl.Columns.Add("Y", 50, HorizontalAlignment.Right)
        lvControl.Columns.Add("XDyn", 50, HorizontalAlignment.Right)
        lvControl.Columns.Add("YDyn", 50, HorizontalAlignment.Right)
        lvControl.Columns.Add("XCon", 50, HorizontalAlignment.Right)
        lvControl.Columns.Add("YCon", 50, HorizontalAlignment.Right)
        lvControl.Columns.Add("CanGlue", 50, HorizontalAlignment.Right)
        lvControl.Columns.Add("Prompt", 50, HorizontalAlignment.Left)

        Try


            ' no drill down
            Dim qryControl = shapeSource.<Control>
            Dim intControls As Integer = qryControl.Count

            For Each ctrl In qryControl

                Dim intID As Integer = -1
                Dim intIX As Integer = -1
                Dim strNameU As String = "NoNameU"

                Dim strX As String = "NoX"
                Dim strY As String = "NoY"
                Dim strXDyn As String = "NoXDyn"
                Dim strYDyn As String = "NoYDyn"
                Dim strXCon As String = "NoXCon"
                Dim strYCon As String = "NoYCon"
                Dim strCanGlue As String = "NoCanGlue"
                Dim strPrompt As String = "NoPropmpt"

                ' first the attributes 
                Dim listOfAttributes As IEnumerable(Of XAttribute) = _
                        From att In ctrl.Attributes() _
                        Select att
                For Each att As XAttribute In listOfAttributes
                    ' Console.WriteLine("{0}={1}", att.Name.LocalName, att.Value)
                    Select Case LCase(att.Name.LocalName)
                        Case "id"
                            intID = CInt(att.Value)
                        Case "ix"
                            intIX = CInt(att.Value)
                        Case "nameu"
                            strNameU = CStr(att.Value)
                    End Select
                Next att

                ' now the element data
                Dim listOfElements As IEnumerable(Of XElement) = _
                    From elem In ctrl.Elements() _
                    Select elem

                If listOfElements.Count > 0 Then
                    For Each elem As XElement In listOfElements
                        ' Console.WriteLine("Elem {0}={1}", elem.Name.LocalName, elem.Value)
                        Select Case LCase(elem.Name.LocalName)
                            Case "x"
                                strX = CStr(elem.Value)
                                Exit Select
                            Case "y"
                                strY = CStr(elem.Value)
                                Exit Select
                            Case "xdyn"
                                strXDyn = CStr(elem.Value)
                                Exit Select
                            Case "ydyn"
                                strYDyn = CStr(elem.Value)
                                Exit Select
                            Case "xcon"
                                strXCon = CStr(elem.Value)
                                Exit Select
                            Case "ycon"
                                strYCon = CStr(elem.Value)
                                Exit Select
                            Case "canglue"
                                strCanGlue = CStr(elem.Value)
                                Exit Select
                            Case "prompt"
                                strPrompt = CStr(elem.Value)
                        End Select
                    Next elem
                End If ' test to make sure we got at least one 

                lvItem = New ListViewItem(intID)
                lvItem.SubItems.Add(CStr(intIX))
                lvItem.SubItems.Add(strNameU)
                lvItem.SubItems.Add(strX)
                lvItem.SubItems.Add(strY)
                lvItem.SubItems.Add(strXDyn)
                lvItem.SubItems.Add(strYDyn)
                lvItem.SubItems.Add(strXCon)
                lvItem.SubItems.Add(strYCon)
                lvItem.SubItems.Add(strCanGlue)
                lvItem.SubItems.Add(strPrompt)

                ' add it to the listview on the data page
                lvControl.Items.Add(lvItem)
                System.Windows.Forms.Application.DoEvents()

            Next ctrl

            lvControl.Refresh()


        Catch errXml As XmlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(errXml, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub



    '
    '
    '
    Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    '
    '
    '
    Private Sub AboutToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        frmAbout.ShowDialog()
    End Sub

End Class